'use strict';

define(['angular', 'moment', 'angular-ui-router', '../../../common/utils/service', 'infiniteScroll', 'angular-material'], function (angular, moment) {
	var module = angular.module('memberAccount', ['ui.router', 'dataService', 'infiniteScroll', 'ngMaterial']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
			.state("member.oldAccount", {
				url: '/member-account',
				views: {
					"@": {
						controller: 'MemberOldAccountCtrl'
					}
				}
			})
		$stateProvider
			.state("member.account", {
				url: '/account',
				views: {
					"@": {
						controller: 'MemberAccountCtrl',
						templateUrl: '/mstore/member/account/account.html'
					}
				}
			})
		$stateProvider
			.state("member.account.details", {
				url: '/details/:id',
				views: {
					"@": {
						controller: 'MemberAccountDetailsCtrl',
						templateUrl: '/mstore/member/account/details.html'
					}
				}
			})
	}]);

	//TODO 以后会删除
	module.controller('MemberOldAccountCtrl', ['$state', function ($state) {
		$state.transitionTo('member.account')
	}])

	module.controller('MemberAccountCtrl', ['$rootScope', '$state', '$scope', 'dataService', function ($rootScope, $state, $scope, dataService) {
		$rootScope.pageTitle = "帐号记录";
		$scope.memberBound(function () {
			var month = moment().format("YYYY-MM");
			$scope.tabs = [{name: '全部', type: 'all'}, {name: '储值', type: 'deposit'}, {name: '支付', type: 'pay'}]
			$scope.selectedIndex = 0;
			$scope.$watch("selectedIndex", function (newValue, oldValue) {
				if (newValue != oldValue)
					$scope.changeType($scope.tabs[newValue].type);
			});
			function getAccountList(type, statusKey, monthDate, cb) {
				if ($scope[statusKey]) {
					return;
				}

				$scope[statusKey] = true;
				dataService.getAccountData(sessionStorage.memberId, type, monthDate, function (err, dataObj) {
					if (err) {
						$scope[statusKey] = false;
						cb && cb(err);
						return;
					}
					$scope.dataObj = dataObj;

					$scope.dataObj.month = dataService.generateMonthData(month, dataObj.txs, "txList", 'createtime');
					$scope[statusKey] = false;
					cb && cb(null, dataObj.txList.length);
				})
			}

			$scope.type = "all";
			getAccountList($scope.type, 'loadingMonthStatus', month);
			$scope.changeType = function (type) {
				getAccountList(type, 'loadingMonthStatus', month);
				$scope.type = type;
			}
			$scope.loadMore = function (cb) {
				month = moment(month).subtract(1, "month").format("YYYY-MM");
				getAccountList($scope.type, 'loadingMoreMonthStatus', month, cb);
			}

			$scope.choseItem = function (item) {
				$state.transitionTo("member.account.details", {id: item.id});
			}
		}, {force: true})
	}]);
	module.controller('MemberAccountDetailsCtrl', ['$state', '$rootScope', '$scope', 'dataService', '$stateParams', function ($state, $rootScope, $scope, dataService, $stateParams) {
		if (!$stateParams.id) {
			return $state.transitionTo("member.account");
		}

		$rootScope.pageTitle = "记录详情";
		dataService.getMemberOneInfo(sessionStorage.memberId, function (err, dataObj) {
			if (err) return;
			$scope.dataObj = dataObj;
			dataService.getOneAccountTx($stateParams.id, function (err, data) {
				if (err) {
					$rootScope.showMessage(data.error);
					return;
				}
				$scope.detail = data;
			})
		})


	}]);
});
